# lingxin-api/main.py
 
# 在 lingxin-api 目录下执行
# pip install fastapi  -i https://pypi.tuna.tsinghua.edu.cn/simple
# pip install uvicorn -i https://pypi.tuna.tsinghua.edu.cn/simple
# 或者安装全套； pip install "fastapi[all]" -i https://pypi.tuna.tsinghua.edu.cn/simple
# 运行方法； python3.9 -m uvicorn main:app --host 0.0.0.0 --port 8000
# uv 虚拟环境 方法
# uv run uvicorn main:app --host 0.0.0.0 --port 8000


from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html
from routers import control  # 你的原路由
from routers import system 

app = FastAPI(
    title="灵芯派 WebAPI",
    description="通过浏览器控制移动、读取传感器",
    version="1.0.0",
    docs_url=None,  # 禁用默认 CDN
    redoc_url=None,
)

# 挂载本地静态资源
app.mount("/static", StaticFiles(directory="static"), name="static")

# 自定义 /docs 路由，使用本地资源
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url="/openapi.json",
        title="灵芯派 API 文档",
        swagger_js_url="/static/swagger-ui-bundle.js",
        swagger_css_url="/static/swagger-ui.css",
    )

# 注册原路由
app.include_router(control.router)
app.include_router(system.router, prefix="/sys", tags=["系统信息"])

# 根路径
@app.get("/")
def root():
    return {"msg": "灵芯派已上线！访问 /docs 查看交互文档"}